package com.ylkj.wx.utils;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public final class CommonUtil {

    /**
     * 获取用户IP
     * @param request
     * @return
     */
    public static String getRemortIP(HttpServletRequest request) {
        if (request.getHeader("x-forwarded-for") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }

    public static  String getIpAddr(HttpServletRequest request)   {
       String ip  =  request.getHeader( " x-forwarded-for " );
       if (ip  ==   null || ip.length()  == 0 || " unknown " .equalsIgnoreCase(ip)){
            ip  =  request.getHeader( " Proxy-Client-IP " );
       }
       if (ip == null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)){
            ip  =  request.getHeader( " WL-Proxy-Client-IP " );
       }
       if (ip  ==   null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)){
           ip  =  request.getRemoteAddr();
       }
       return  ip;
    }

    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
    
    public static boolean isMobileNO(String mobiles){
        boolean flag = false;
        if(StringUtils.isNotBlank(mobiles)){
            Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
            Matcher m = p.matcher(mobiles);
            System.out.println(m.matches()+"---");
            if(m.matches()){
                flag = true;
            }
        }
        return flag;
    }
}
